//
//  XDHomeHabitViewController.m
//  XDEducation
//
//  Created by apple on 2017/4/7.
//  Copyright © 2017年 apple. All rights reserved.
//

#import "XDHomeHabitViewController.h"
#import "XDHabitData.h"
#import "XDHabitBannerCell.h"
#import "XDHomeButtonItemCell.h"
#import "XDHomeCellHeaderView.h"
#import "XDHabitWorldCell.h"
#import "XDHomeVideoCell.h"
#import "XDVideoModel.h"
#import "XDVideoPlayViewController.h"
#import "XDHomeButtonItemsCell.h"
#define kOneCell @"cell"
#import "XDHttpTool.h"
#import "NSString+ChangeFormat.h"
#import "WebViewController.h"
#import "XDCelebrityController.h"
#import "XDMangaStarsController.h"
#import "XDGradeVideoController.h"
@interface XDHomeHabitViewController ()

/** 页面控件数据源 */
@property(nonatomic,strong) NSMutableArray <NSDictionary *>* habitDataArr;

/** 轮播图数据源 */
@property(nonatomic,strong) NSMutableArray <NSString *>*bannerArr;

/** 好成绩视频数据源 */
@property(nonatomic,strong) NSMutableArray <XDVideoModel *>*videoModelArr;

@end

static NSString *const competitionCellId = @"competitionCell";
static NSString *const readCellId = @"readCell";
static NSString *const habitWorldCellId = @"HabitWorldCell";
static NSString *const viedoCellId = @"ViedoCell";

@implementation XDHomeHabitViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    self.tableView.frame = CGRectMake(0, 44, ScreenW, ScreenH - 44 - 64 -44);
    [self initNetworkingWithRefreshStatus:RefreshStatePullDown];
    

}

#pragma mark - 懒加载

-(NSMutableArray<NSString *> *)bannerArr{

    if (!_bannerArr) {
        _bannerArr = [NSMutableArray arrayWithObject:@"http://resource.xing6688.com/data/image/header/0/22/7_39690.jpg"];
    }
    return _bannerArr;
}

-(NSMutableArray<NSDictionary *> *)habitDataArr{
    if (_habitDataArr == nil) {
        
        _habitDataArr = [NSMutableArray array];
        _habitDataArr = [XDHabitData getHabitData];
    }
    return _habitDataArr;
}

#pragma mark - tableview delegate

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{

    return self.habitDataArr.count;
}


-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

    return 1;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 5;
}


-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    
    if (section == 0)
        return nil;
    
    NSString *title = self.habitDataArr[section][@"title"];
    
    BOOL isShowMoreBtn = NO;
    
    if (section == 3) {
        
        isShowMoreBtn = YES;
    }
    
    XDHomeCellHeaderView *headerView =  [[XDHomeCellHeaderView alloc]initWithTitle:title MoreBtnStatus:isShowMoreBtn];
    headerView.moreBlock = ^(){
        if ([title isEqualToString:@"习惯视频"])
        {
            XDGradeVideoController *VC = [[XDGradeVideoController alloc] init];
            VC.XDGradeVideoControllerType = HaoSuYang;
            VC.title = title;
            [self.navigationController pushViewController:VC animated:YES];
        }
    };
    return headerView;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    if (section == 0) return 0;
    return 44 * ScaleHeight;
}


-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{

    switch (indexPath.section) {
        case 0:
            return 150 * ScaleHeight;
            break;
        case 1:
            return 100 * ScaleHeight;
            break;
        case 2:
            return 60 * ScaleHeight;
            break;
        case 3:
            return 170 * ScaleHeight;
            break;
        case 4:
            return 210 * ScaleHeight;
            break;
        default:
            return 0;
            break;
    }
}
-(void)cancelJoinNetworkWithDataId:(NSInteger)dataId index:(NSInteger)index{
    
    NSString *url;
  
   
        url = BaseURL(@"organizationMember.do?action=cancelJoinOrganization");
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    [params setValue:@"XXDD_FDGG" forKey:@"forumcode"];
    [params setValue:@"showallCompetition" forKey:@"action"];
    [params setValue:@"6" forKey:@"ptype"];

    
    [MBProgressHUD showHUD];
    [XDHttpTool get:url authCode:[AccountTool shareAccountTool].currentAccount.authCode params:params success:^(id responseObj) {
        [MBProgressHUD hideHUD];
        
        NSLog(@"%@",responseObj);
        
    } failure:^(NSError *error) {
        [MBProgressHUD hideHUD];
        [MBProgressHUD showError:XDNetWorkError];
        NSLog(@"%@",error);
    }];
}


-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

  
    switch (indexPath.section) {
        case 0:{   //轮播图
            
            XDHabitBannerCell *cell = [XDHabitBannerCell habitBannerCellWithTableView:tableView imageArr:self.bannerArr];
            
            return cell;
        }
            break;
            
        case 1:{    //三好竞赛
            
            
            
        
            NSMutableArray *itemArr = self.habitDataArr[1][@"item"];
            XDHomeButtonItemCell *cell = [XDHomeButtonItemCell cellWithTableView:tableView dataArr:itemArr cellId:competitionCellId];
            [cell setItemBlock:^(UIViewController *vc, NSString *vcTitle) {
                
                
                
                if ([vcTitle isEqualToString:@"学习竞赛"]) {

                    NSString *str = [[NSString alloc] initWithFormat:@"%@",Base@"bbsws.do?action=showallCompetition&forumcode=XXDD_FDGG&ptype=6"];
                    [self theURL:str title:vcTitle];
                }else if ([vcTitle isEqualToString:@"趣味问答"]){

                    NSString *str = [[NSString alloc] initWithFormat:@"%@",Base@"bbsws.do?action=showallCompetition&forumcode=XXDD_FDGG&ptype=7"];
                   [self theURL:str title:vcTitle];
        
                   
                }else if ([vcTitle isEqualToString:@"竞赛公告"]){
                    
                      NSString *str = [[NSString alloc] initWithFormat:@"%@",Base@"bbsws.do?action=postpage&topicid=5386"];
                    [self theURL:str title:vcTitle];
                   
                }else{
                    
                    NSString *str = [[NSString alloc] initWithFormat:@"%@",Base@"bbsws.do?action=showalltopic&forumcode=XXDD_XXSQ_GRJZ_GRB&pageNumber=1"];
                    [self theURL:str title:vcTitle];
                   
                    
                }
    
            }];
        
            return cell;
        }
            break;
        case 2:{    //习惯天地
            
            [tableView registerNib:[UINib nibWithNibName:@"XDHabitWorldCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:habitWorldCellId];
            
            XDHabitWorldCell *cell = [tableView dequeueReusableCellWithIdentifier:habitWorldCellId];
            [cell setItemBlock:^(NSString *vcTitle) {
                
                if ([vcTitle isEqualToString:@"学习习惯"]) {
                    NSString *str = [[NSString alloc] initWithFormat:@"%@",Base@"happyreadingws.do?action=getAllHappyReadPage&forumcode=XXDD_QDLY_XGTD_XXXG&pageNumber=1"];
                    [self theURL:str title:vcTitle];
                    
                }else if ([vcTitle isEqualToString:@"生活习惯"]){
                    NSString *str = [[NSString alloc] initWithFormat:@"%@",Base@"happyreadingws.do?action=getAllHappyReadPage&forumcode=XXDD_QDLY_XGTD_WSAQ&pageNumber=1"];
                    [self theURL:str title:vcTitle];
                }else{
                    NSString *str = [[NSString alloc] initWithFormat:@"%@",Base@"happyreadingws.do?action=getAllHappyReadPage&forumcode=XXDD_QDLY_XGTD_ZWGL&pageNumber=1"];
                    [self theURL:str title:vcTitle];
                    
                }
                
            }];
            
            
            return cell;
        }
            break;
        case 3:{   //习惯视频
            
            [tableView registerNib:[UINib nibWithNibName:@"XDHomeVideoCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:viedoCellId];
            
            XDHomeVideoCell *cell = [tableView dequeueReusableCellWithIdentifier:viedoCellId];
            WeakSelf(weakself)
            cell.videoArr = self.videoModelArr;
            cell.videoClickedBlock = ^(XDVideoModel *model){
                
                if (model.absoluteUrl.length == 0) {
                    return ;
                }
                XDVideoPlayViewController *vc = [[XDVideoPlayViewController alloc]init];
                vc.videoModel = model;
                [weakself presentViewController:vc animated:YES completion:nil];
            };
            return cell;
        }
            break;
        case 4:{    //快乐阅读星
            
            NSMutableArray *itemArr = self.habitDataArr[4][@"item"];
            XDHomeButtonItemCell *cell = [XDHomeButtonItemCell cellWithTableView:tableView dataArr:itemArr cellId:readCellId];
        
            [cell setItemBlock:^(UIViewController *vc, NSString *vcTitle) {
                
                if ([vcTitle isEqualToString:@"寓言故事"]) {
                    NSString *str = [[NSString alloc] initWithFormat:@"%@",Base@"bbsws.do?action=showalltopic&forumcode=XXDD_QDLY_KLYD_YY&pageNumber=1"];
                    [self theURL:str title:vcTitle];
                    
                    
                }else if ([vcTitle isEqualToString:@"优秀作文"]){
                    NSString *str = [[NSString alloc] initWithFormat:@"%@",Base@"bbsws.do?action=showalltopic&forumcode=XXDD_QDLY_KLYD_YXZW&pageNumber=1"];
                    [self theURL:str title:vcTitle];
                    
                }else if ([vcTitle isEqualToString:@"短篇小说"]){
                    NSString *str = [[NSString alloc] initWithFormat:@"%@",Base@"bbsws.do?action=showalltopic&forumcode=XXDD_QDLY_KLYD_DPXS&pageNumber=1"];
                    [self theURL:str title:vcTitle];
                }else if ([vcTitle isEqualToString:@"幽默笑话"]){
                    NSString *str = [[NSString alloc] initWithFormat:@"%@",Base@"bbsws.do?action=showalltopic&forumcode=XXDD_QDLY_KLYD_YMXH&pageNumber=1"];
                    [self theURL:str title:vcTitle];
                }else if ([vcTitle isEqualToString:@"有声阅读"]){
                    NSString *str = [[NSString alloc] initWithFormat:@"%@",Base@"bbsws.do?action=showalltopic&forumcode=XXDD_QDLY_KLYD_MJZL&pageNumber=1"];
                    [self theURL:str title:vcTitle];
                }else if ([vcTitle isEqualToString:@"我的长辈"]){
                    NSString *str = [[NSString alloc] initWithFormat:@"%@",Base@"bbsws.do?action=showalltopic&forumcode=XXDD_QDLY_KLYD_WSZB&pageNumber=1"];
                    [self theURL:str title:vcTitle];
                }else if ([vcTitle isEqualToString:@"名人名家"]){
                    XDCelebrityController *celebrity = [XDCelebrityController new];
                    celebrity.titleStr = vcTitle;
                    
                    [self.navigationController pushViewController:celebrity animated:YES];
                }else{
                    XDMangaStarsController *mangaStars = [XDMangaStarsController new];
                    mangaStars.nameTitle = vcTitle;
                    [self.navigationController pushViewController:mangaStars animated:YES];
                }
                
                
            }];
            
            
            return cell;
        }
            
            break;
        default:{
            return nil;
        }
            break;
    }
    
}



-(void)initNetworkingWithRefreshStatus:(RefreshState)status{
    
    //media.do
    //[page=1 | action=videolist | vtype=2 | type=0]
    
    //vtype=0 好成绩  vtype=1好习惯  vtype=2 好素养
    NSString *url = BaseURL(@"media.do?action=videolist");
    NSDictionary *dict = @{@"type":@0,@"vtype":@1,@"page":@1};
    [XDHttpTool get:url authCode:[AccountTool shareAccountTool].currentAccount.authCode params:dict success:^(id responseObj) {
        
        //NSLog(@"%@",responseObj);
        NSArray *dictArr = [responseObj objectForKey:@"dataList"];
        NSMutableArray *modelArr = [XDVideoModel mj_objectArrayWithKeyValuesArray:dictArr];
        self.videoModelArr = modelArr;
        [self.tableView reloadData];
        
    } failure:^(NSError *error) {
        
        NSLog(@"%@",error);
    }];
    
}


- (void)theURL:(NSString *)str title:(NSString *)title{
    
    WebViewController *web = [WebViewController new];
    
    web.str = str;
    web.nameTitle = title;
    [self.navigationController pushViewController:web animated:YES];
}

- (void)theURL:(NSString *)str andTheForumcode:(NSString *)url andTheFunCode:(NSString *)funCode title:(NSString *)title{
    WebViewController *web = [WebViewController new];
    
    web.str = str;
    web.nameTitle = title;
    [self.navigationController pushViewController:web animated:YES];
}













@end
